package com.example.onlinemusic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.onlinemusic.common.R;
import com.example.onlinemusic.mapper.SingerMapper;
import com.example.onlinemusic.model.domain.Singer;
import com.example.onlinemusic.model.request.SingerRequest;
import com.example.onlinemusic.service.ObjectStoreService;
import com.example.onlinemusic.service.SingerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 歌手管理接口实现类
 */
@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {

    @Autowired
    SingerMapper singerMapper;

    @Autowired
    private ObjectStoreService oss;

    @Override
    public R allSinger() {
        return R.success("查询成功", singerMapper.selectList(null));
    }

    @Override
    public R addSinger(SingerRequest singerRequest) {
        // 要插入到数据库的歌手对象
        Singer singer = new Singer();
//        singer.setName(singerRequest.getName());
//        singer.setSex(singerRequest.getSex());
//        singer.setBirth(singerRequest.getBirth());
//        singer.setIntroduction(singerRequest.getIntroduction());
//        singer.setLocation(singerRequest.getLocation());
//        singer.setPic(singerRequest.getPic());
        // 属性复制
        BeanUtils.copyProperties(singerRequest, singer);
        //给出默认头像,后续管理员可以在线修改
        String pic = "https://java-pp.oss-cn-hangzhou.aliyuncs.com/img/singerPic/user.jpg";
        singer.setPic(pic);
        // 将数据保存到数据库Singer表中
        int result = singerMapper.insert(singer);
        if (result > 0) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @Override
    public R deleteSinger(int id) {
        int i = singerMapper.deleteById(id);
        if (i > 0) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    @Override
    public R updateSinger(SingerRequest singerRequest) {
        Singer newSinger = new Singer();
        BeanUtils.copyProperties(singerRequest, newSinger);
        int i = singerMapper.updateById(newSinger);
        if (i > 0) {
            return R.success("保存成功");
        } else {
            return R.error("保存失败");
        }
    }

    @Override
    public R updateSingerPic(MultipartFile avatorFile, int id) {
        String fileName = System.currentTimeMillis() + "-" + avatorFile.getOriginalFilename();
        File dest = new File(fileName + System.getProperty("file.separator") + fileName);
        String imgPath = "img/singerPic";
        // 使用阿里云对象存储服务替换原来的存储在本地的流程
        String ossFilePath;
        try {
            ossFilePath = oss.uploadFile(imgPath, fileName, avatorFile);
        } catch (IOException e) {
            return R.fatal("上传失败" + e.getMessage());
        }

        Singer singer = new Singer();
        singer.setId(id);
        singer.setPic(ossFilePath);
        if (singerMapper.updateById(singer) > 0) {
            return R.success("上传成功", ossFilePath);
        } else {
            return R.error("上传失败");
        }
    }


}
